package cn.crex1.xiaomi.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static LocalDateTime newNowDateTime() {
		return LocalDateTime.now();
	}

	/**
	 * 将localdatetime转换成date
	 * @param time
	 * @return
	 */
	public static Date converDateByDateTime(LocalDateTime time) {
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = time.atZone(zoneId);
		Date date = Date.from(zdt.toInstant());
		return date;
	}
	/**
	 * 将date转成localdatetime
	 * @param time
	 * @return
	 */
	public static LocalDateTime converDateTimeByDate(Date time) {
		Instant instant = time.toInstant();
		ZoneId zoneId = ZoneId.systemDefault();
		LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
		return localDateTime;
	}

	/**
	 * 时期格式化
	 * @param time
	 * @return
	 */
	public static String formatDateTime(LocalDateTime time) {
		DateTimeFormatter Pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String format = time.format(Pattern);
		return format;
	}
}
